package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.Card;
import com.geyue.core.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by Lyon on 2017/5/17.
 */
@Controller
@RequestMapping("/card")
public class CardController {

    @Autowired
    private CardService cardService;

    @RequestMapping(value = "/jd", method = RequestMethod.GET)
    public String jd() {
        return "card/jd";
    }

    @RequestMapping(value = "/rp", method = RequestMethod.GET)
    public String rp() {
        return "card/rp";
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public Res list(Integer type) {

        if(type == Card.RP){
            return Res.success("", cardService.getRp());
        }else {
            return Res.success("", cardService.getJd());
        }
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public Res list(@Validated Card card, BindingResult binding) {

        if(binding.hasErrors())
            return Res.fail("参数不正确!");

        if(cardService.saveNotEmpty(card) > 0)
            return Res.success("添加成功!");

        return Res.fail("添加失败!");
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public Res edit(Card card) {

        if(cardService.updateNotEmpty(card) > 0)
            return Res.success("修改成功!");

        return Res.fail("修改失败!");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Res del(@PathVariable Integer id) {

        if(cardService.deleteByKey(id) > 0)
            return Res.success("删除成功!");

        return Res.fail("删除失败!");
    }

}
